import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams, LoadingController, ToastController} from 'ionic-angular';
import {LoadingService} from "../../../services/common/loading.service";
import {ToastService} from "../../../services/common/toast.service";
import {StaffOnjobProvider} from '../../../providers/my-team/team-staff-onjob/team-staff-onjob.provider';
import * as moment from 'moment';
import {AdpActionSheetController} from '../../../components/common/adp-action-sheet/adp-action-sheet-controller';

@IonicPage()
@Component({
    selector:'team-staff-onjob',
    templateUrl:'team-staff-onjob.html',
    providers:[
        StaffOnjobProvider,
        LoadingService,
        ToastService
    ]
})

export class TeamStaffOnjobPage {
    //0默认为全部显示
    public showGroup:string='0';
    //业务组,0默认为'全部'
    public groupArr:any[]=[];
    //全部员工信息
    public staffs:any[];
    //滚动监听
    public isScrolling:boolean=false;
    //加载监听
    public isLoaded:boolean=false;

    @ViewChild(Content) content:Content;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        private load:LoadingService,
        private toast:ToastService,
        private staffOnjobProvider:StaffOnjobProvider,
        private actionSheetCtrl:AdpActionSheetController
    ){}

    ionViewDidLoad() {
        // this.staffs=this.sortArr(this.staffOnjobProvider.getStaffInfo()); //测试
        this.getTotalMember();
    }

    /**
     * 获取员工信息
    */
    getTotalMember(){
        this.load.showLoading();
        this.staffOnjobProvider.getStaffInfo().subscribe(data=>{
            this.isLoaded=true;
            this.staffs=this.sortArr(data);
            this.load.dismissLoading();
        },()=>{
            this.load.dismissLoading();
        });
    }

    /**
     * 生日,周年排序,过滤重复业务组
     * */
    sortArr(arr) {
        let birArr=[],annArr=[],otherArr=[],tempArr=[];
        arr.forEach(item=>{
            if(tempArr.indexOf(item.groupId)===-1){
                tempArr.push(item.groupId);
                this.groupArr.push(item);
            }
            //修改时间格式
            item.birthday=item.birthday.replace(/-/g,'/');
            // if(item.isBirthday === 1)
            //     birArr.push(item);
            // else if(item.isAnniversary === 1)
            //     annArr.push(item);
            // else
            //     otherArr.push(item);
        })

        // return birArr.concat(annArr,otherArr);
        return arr;
    }

    /**
     * 选组
     */
    showBusinessGroup(event) {
        let temGroup = [];
        this.groupArr.forEach((item) => {
            temGroup.push({
                cssClass: item.groupId === this.showGroup ? 'action-sheet-selected' : '',
                text: item.groupName,
                handler: () => {
                    this.showGroup = item.groupId;
                }
            })
        });
        temGroup.push({
            text: '全部',
            cssClass: this.showGroup === '0' ? 'action-sheet-selected' : '',
            handler:()=>{
                this.showGroup='0';
            }
        });
        this.actionSheetCtrl.create({
            title:'所辖组',
            cssClass: 'roster-action',
            buttons: temGroup
        }).present();
        event.stopPropagation();
    }

    /**
     * 返回
     */
    backToProPage() {
        this.navCtrl.pop(null);
    }

    /**
     * 回到顶部
     */
    goUp() {
        this.content.scrollToTop();
    }
}
